from typing import AsyncGenerator

from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from src.config import DB_HOST, DB_NAME, DB_PASS, DB_PORT, DB_USER

# DB_HOST= "localhost"
# DB_PORT= "5432"
# DB_NAME= "postgres"
# DB_USER= "postgres"
# DB_PASS= "Qq12345"
# SECRET2 = "SECRET2"
# SECRET3 = "SECRET3"

# Проверка на None для переменных окружения
if not all([DB_HOST, DB_NAME, DB_PASS, DB_PORT, DB_USER]):
    raise ValueError("One or more database configuration variables are not set")


DATABASE_URL = f"postgresql+asyncpg://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
Base = declarative_base()


#
engine = create_async_engine(DATABASE_URL)
async_session_maker = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)


async def get_async_session() -> AsyncGenerator[AsyncSession, None]:
    async with async_session_maker() as session:
        yield session
